package com.demo.auth.controller;

import com.nimbusds.jose.jwk.JWKSet;
import com.nimbusds.jose.jwk.RSAKey;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.security.KeyPair;
import java.security.interfaces.RSAPublicKey;
import java.util.Map;

@Slf4j
@RestController()
public class KeyPairController {

    @Resource
    private KeyPair keyPair;

    /**
     * 接口提供给网关调用，提供publicKey给网关做鉴权用
     * @return 返回jwks
     */
    @GetMapping("/rsa/publicKey")
    public Map<String, Object> getKey() {
        log.info("获取publicKey");
        RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
        RSAKey key = new RSAKey.Builder(publicKey).build();
        return new JWKSet(key).toJSONObject();
    }
}
